package com.zxs.carousel.service.impl;

import com.zxs.carousel.mapper.CarouselMapper;
import com.zxs.carousel.service.CarouselService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Carousel;
import utils.R;

import java.util.List;

/**
 * @author zxs
 * *7 14:21
 */
@Service
@Slf4j
public class CarouselServiceImpl implements CarouselService {

    @Autowired
    private CarouselMapper carouselMapeer;

    /**
     * 查询优先级最高的数据，并返回轮播最大图片数量6张
     *
     * @return
     */
    @Override
    public R list() {
        //根据表的字段priority作为查询条件降序排列
        int limit = 6; //至多查询四条

        //查询数据库
        IPage<Carousel> iPage = new Page<>(1, limit);
        QueryWrapper<Carousel> carouselQueryWrapper = new QueryWrapper<>();
        carouselQueryWrapper.orderByDesc("priority");
        IPage<Carousel> page = carouselMapeer.selectPage(iPage, carouselQueryWrapper);

        List<Carousel> carouselList = page.getRecords();

        return R.ok(carouselList);

    }
}
